package main.java.DMDE.view;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ButtonGroup;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;

import com.jgoodies.forms.factories.CC;
import com.jgoodies.forms.layout.FormLayout;

import main.java.DMDE.controllers.IClassController;
import main.java.DMDE.model.Modifier;
import main.java.DMDE.model.Visibility;

public class ClassView extends CommonEntityView implements IClassView {

    private IClassController classController;
    private IAttributeView attribView;
    protected static String TITLE = "Class Creation";

    private JCheckBox absCheckBox;
    private JCheckBox finalCheckBox;
    private JLabel visibCondition;
    private JRadioButton btnPublic;
    private JRadioButton btnPrivate;
    private JRadioButton btnProtected;

    public ClassView() {
	super();
	initClassComponents();
	createButton.addActionListener(new ActionListener() {
	    @Override
	    public void actionPerformed(ActionEvent actionEvent) {
		createClass();
	    }
	});
    }

    private void initClassComponents() {
	visibCondition = new JLabel();
	visibCondition.setText("Visibility: ");
	initComponents();
	btnPublic.doClick();
	frame1.setTitle(TITLE);
    }

    protected void initGeneralPanel() {
 	frame1ContentPane.setLayout(new FormLayout("74*(default, $lcgap), default",
			"73*(default, $lgap), default"));
	generalPanel = new JPanel();
	classNameText = new JLabel();
	className = new JTextField();
	absCheckBox = new JCheckBox();
	finalCheckBox = new JCheckBox();
	btnPublic = new JRadioButton("Public");
	btnPrivate = new JRadioButton("Private");
	btnProtected = new JRadioButton("Protected");
	ButtonGroup group = new ButtonGroup();
	group.add(btnPrivate);
	group.add(btnProtected);
	group.add(btnPublic);
	classNameText.setText("Class name: ");
	classNameText.setLabelFor(className);
	absCheckBox.setText("Abstract");
	visibCondition.setText("Visibility: ");
	finalCheckBox.setText("Final");

	
	GroupLayout generalPanelLayout = new GroupLayout(generalPanel);
	generalPanel.setLayout(generalPanelLayout);

	generalPanelLayout.setAutoCreateGaps(true);
	generalPanelLayout.setAutoCreateContainerGaps(true);

	generalPanelLayout.setHorizontalGroup(generalPanelLayout
		.createSequentialGroup()
		.addGroup(
			generalPanelLayout
				.createParallelGroup(
					GroupLayout.Alignment.LEADING)
				.addComponent(classNameText)
				.addComponent(absCheckBox)
				.addComponent(visibCondition)
				.addComponent(btnPublic))

		.addGroup(
			generalPanelLayout
				.createParallelGroup(
					GroupLayout.Alignment.LEADING)
				.addComponent(className)
				.addComponent(finalCheckBox)
				.addComponent(btnPrivate))
		.addGroup(
			generalPanelLayout
				.createParallelGroup(
					GroupLayout.Alignment.LEADING)
				.addComponent(className)
				.addComponent(btnProtected)));
	generalPanelLayout.setVerticalGroup(generalPanelLayout
		.createSequentialGroup()
		.addGap(30)
		.addGroup(
			generalPanelLayout
				.createParallelGroup(
					GroupLayout.Alignment.BASELINE)
				.addComponent(classNameText)
				.addComponent(className))
		.addGap(30)
		.addGroup(
			generalPanelLayout
				.createParallelGroup(
					GroupLayout.Alignment.BASELINE)
					.addComponent(absCheckBox)
					.addComponent(finalCheckBox))
		.addGap(30)
		.addComponent(visibCondition)
		.addGap(20)
		.addGroup(
			generalPanelLayout
				.createParallelGroup(
					GroupLayout.Alignment.BASELINE)
				.addComponent(btnPublic)
				.addComponent(btnPrivate)
				.addComponent(btnProtected)));
    }

    @Override
    public void setClassName(String name) {
	this.className.setText(name);
    }

    @Override
    public void setController(IClassController classController) {
	this.classController = classController;
    }

    private void createClass() {
	if (classController.addClass()) {
	    this.frame1.setVisible(false);
	} else {
	    showWrongClassNameDialog();
	}

    }

    @Override
    public String getClassName() {
	return className.getText();
    }

    @Override
    public void addAttributeView(IAttributeView attributeView) {
	this.attribView = attributeView;
	JPanel attributePanel = attribView.getPanel();
	tabbedPane.addTab("Attribute", attributePanel);
    }

    @Override
    public void addOperationView(IOperationView operatView) {
	addCommonOperationView(operatView); 
    }

    @Override
    public Visibility getVisibility() {
	if (btnPrivate.isSelected())
	    return Visibility.PRIVATE;
	else {
	    if (btnProtected.isSelected())
		return Visibility.PROTECTED;
	    else
		return Visibility.PUBLIC;
	}
    }

    @Override
    public List<Modifier> getModifiers() {
	List<Modifier> modifiers = new ArrayList<Modifier>();
	if (finalCheckBox.isSelected())
	    modifiers.add(Modifier.FINAL);
	if (absCheckBox.isSelected())
	    modifiers.add(Modifier.ABSTRACT);
	return modifiers;
    }

    @Override
    public void setVisibility(Visibility visibility) {
	if (visibility.getType().equals(Visibility.PRIVATE.getType()))
	    btnPrivate.doClick();
	else {
	    if (visibility.getType().equals(Visibility.PROTECTED.getType()))
		btnProtected.doClick();
	    else
		btnPublic.doClick();
	}
	
    }

    @Override
    public void setModifiers(List<Modifier> modifiers) {
	for(Modifier mod : modifiers){
	    if (mod.getDesc().equals(Modifier.FINAL.getDesc()))
		    finalCheckBox.doClick();
		else {
		    if (mod.getDesc().equals(Modifier.ABSTRACT.getDesc()))
			absCheckBox.doClick();
		}   
	}
	
    }
}
